<template>
  <div>
    <common-head title="搜索页"/>
    <div class="wrap">
      <hr>
      <ul class="nav-wrap">
        <li @click="switchTab(0)" :class="{active: active === 0}">综合</li>
        <li @click="switchTab(1)" :class="{active: active === 1}">销量</li>
        <li @click="orderByPrice" :class="['price', {active: active === 2}]">
          <span>价格</span>
          <div :class="['angle', 'up', {active: active === 2 && priceUpDown === 0}]"></div>
          <div :class="['angle', 'down', {active: active === 2 && priceUpDown === 1}]"></div>
        </li>
        <li @click="switchTab(3)" :class="{active: active === 3}">新品</li>
      </ul>
      <hr>
      <div class="items">
        <div class="item"
          v-for="(item, index) in items"
          :key="item.id"
          @click="clickItem(item.id, index)"
          >
          <img :src="item.pic" alt="">
          <div class="info">
            <h5 class="name">{{item.name}}</h5>
            <p class="desc">{{item.characteristic?item.characteristic:'暂无描述' | filterDesc }}</p>
            <div class="price-tubiao">
              <span class="price">￥{{item.minPrice}}</span>
              <van-icon class-prefix="icon" name="gouwuche" color="red" size="20"/>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { fetchItem } from 'api'
import CommonHead from 'components/CommonHead'
export default {
  name: 'itemList',
  data () {
    return {
      active: 0,
      priceUpDown: 0, // 0是升序，1是降序
      items: []
    }
  },
  created () {
    this.fetchItem()
  },
  methods: {
    clickItem (id) {
      // 进入详情页
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    fetchItem () {
      fetchItem({
        k: this.$route.query.keyword,
        orderBy: this.orderBy
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
      })
    },
    switchTab (index) {
      this.active = index
      this.fetchItem()
    },
    orderByPrice () {
      if (this.active !== 2) {
        this.active = 2
        this.priceUpDown = 0
      } else {
        if (this.priceUpDown === 0) {
          this.priceUpDown = 1
        } else {
          this.priceUpDown = 0
        }
      }
      this.fetchItem()
    }
  },
  filters: {
    filterDesc (v, len = 10) {
      if (v.length > 10) {
        return v.substr(0, len) + '...'
      } else {
        return v
      }
    }
  },
  computed: {
    orderBy () {
      let order = ''
      switch (this.active) {
        case 0:
          // 综合
          order = ''
          break
        case 1:
          // 销量
          order = 'ordersDown '
          break
        case 2:
          // 价格
          if (this.priceUpDown === 0) {
            order = 'priceUp'
          } else {
            order = 'priceDown'
          }
          break
        case 3:
          // 新品
          order = 'addedDown'
          break
      }
      return order
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.wrap{
  .nav-wrap{
    line-height: 44px;
    display: flex;
    justify-content: space-around;
    background-color: #fff;
    font-size: 14px;
    color: rgb(97, 96, 96);
    li.active{
      color: #f88;
    }
    .price{
      position: relative;
      .angle{
        width: 0;
        height: 0;
        position: absolute;
        right: -15px;
        border: 6px solid transparent;
        &.up{
          border-bottom-color:#ccc;
          top:8px;
          &.active{
            border-bottom-color:#f88;
          }
        }
        &.down{
          border-top-color:#ccc;
          bottom:9px;
          &.active{
            border-top-color:#f88;
          }
        }
      }
    }
  }
  .item{
    padding:8px;
    background-color: #fff;
    height: 200px;
    display: flex;
    padding: 5px;
    border-radius: 8px;
      img{
        width: 180px;
        height: 180px;
      }
    .info{
      flex: 1;
      padding-left: 20px;
      .name{
        padding-top: 30px;
        line-height: 30px;
      }
      .desc{
        font-size: 12px;
        color: #333;
        padding-top: 20px;
      }
      .price-tubiao{
        margin-top: 30px;
        line-height: 30px;
        display: flex;
        justify-content: space-between;
        padding-right: 10px;
      }
    }
  }
}
</style>
